package com.ln.asset.core.controller.admin;



import com.ln.asset.common.exception.Assert;
import com.ln.asset.common.result.R;
import com.ln.asset.common.result.ResponseEnum;
import com.ln.asset.core.pojo.Dict;
import com.ln.asset.core.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author 卢光云
 * @since 2022-04-28
 */
@Api(tags = "分类管理")
@RestController
@RequestMapping("/admin/core/asset")
@Slf4j
public class AdminDictController {
    @Resource
    private DictService dictService;
    @ApiOperation("根据上级id获取子节点数据列表")
    @GetMapping("/listByParentId/{parentId}")
    public R listByParentId(
            @ApiParam(value = "上级节点id",required = true)
            @PathVariable Integer parentId){
        List<Dict> assList=dictService.listByParentId(parentId);
        return R.ok().data("list",assList);
    }

    @ApiOperation("新增分类")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "所属分类", required = true)
            @RequestBody Dict dict){
        Assert.notNull(dict.getName(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = dictService.save(dict);
        if (result) {
            return R.ok().message("保存成功");
        }else {
            return R.ok().message("保存失败");
        }
    }
    @ApiOperation(value = "根据id删除", notes="逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(value = "数据id", required = true, example = "1")
            @PathVariable Integer id){
        boolean result = dictService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }
}